<template>
    <div class="app-container">
        <div class="searchAll" slot="search">
            <el-form :model="queryParams" ref="queryForm" label-position="right" v-show="showSearch" label-width="68px">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-form-item :label="$t('notice.noticeTitle')" prop="noticeTitle" size="small">
                            <el-input v-model="queryParams.noticeTitle" :placeholder="$t('public.placeholder',{placeholder:$t('notice.noticeTitle')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('notice.operator')" prop="createBy" size="small">
                            <el-input v-model="queryParams.createBy" :placeholder="$t('public.placeholder',{placeholder:$t('notice.operator')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('notice.noticeType')" prop="noticeType" size="small">
                            <el-select v-model="queryParams.noticeType" :placeholder="$t('public.placeholder',{placeholder:$t('notice.noticeType')})" clearable size="small">
                                <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <div class="searchAres searchAresHeng">
                            <el-button type="primary" icon="el-icon-search" size="mini" class="searchBtn" @click="handleQuery">{{ $t("public.query") }}</el-button>
                            <el-button class="resectBtn" icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("public.reset") }}</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="tableList">
            <div class="insTitle">
                <span>{{ $t("systemTitle.Notice") }}</span>
            </div>
            <div class="btnGroup">
                <el-button v-hasPermi="['tenant:notice:add']" type="primary" icon="el-icon-plus" size="mini" class="additemBtn" @click="handleAdd">{{ $t("public.add") }}</el-button>
                <el-button v-hasPermi="['tenant:notice:remove']" icon="el-icon-delete" size="mini" class="deleteitemBtn" :disabled="multiple" @click="handleDelete">{{ $t("public.delete") }}</el-button>

            </div>
            <el-table max-height="350" border v-loading="loading" stripe :data="noticeList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />

                <el-table-column :label="$t('public.serialNum')" align="center" prop="noticeId" width="170" />
                <el-table-column :label="$t('notice.noticeTitle')" align="center" prop="noticeTitle" width="150" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('notice.noticeContent')" prop="noticeContent" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('notice.noticeType')" align="center" prop="noticeType" width="100">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_notice_type" :value="scope.row.noticeType" />
                    </template>
                </el-table-column>

                <el-table-column :label="$t('public.creator')" align="center" prop="createBy" width="100" />
                <el-table-column :label="$t('public.createTime')" align="center" prop="createTime" width="100">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('public.status')" align="center" prop="status" fixed="right" width="80">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('public.opera')" align="center" fixed="right" width="100">
                    <template slot-scope="scope">
                        <div class="action-wrap">

                            <table-body-btn type="gwedit" :name="$t('public.edit')" @click.native="handleUpdate(scope.row)" v-hasPermi="['tenant:notice:edit']" />
                            <table-body-btn type="gwdelete" :name="$t('public.delete')" @click.native="handleDelete(scope.row)" v-hasPermi="['tenant:notice:remove']" />
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
        </div>

        <!-- 添加或修改公告对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body :fullscreen="fullscreen">
            <DialogTit slot="title" :title="title" :fullscreen.sync="fullscreen"></DialogTit>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item :label="$t('notice.noticeTitle')" prop="noticeTitle" size="small">
                            <el-input v-model="form.noticeTitle" :placeholder="$t('public.placeholder',{placeholder:$t('notice.noticeTitle')})" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('notice.noticeType')" prop="noticeType" size="small">
                            <el-select v-model="form.noticeType" :placeholder="$t('public.placeholder',{placeholder:$t('notice.noticeType')})">
                                <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('public.status')" size="small">
                            <el-radio-group v-model="form.status">
                                <el-radio v-for="dict in dict.type.sys_notice_status" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('public.content')" size="small">
                            <editor v-model="form.noticeHtml" :min-height="192" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel" size="small" icon="el-icon-close">{{ $t('public.cancel') }}</el-button>
                <el-button type="primary" @click="submitForm" size="small" icon="el-icon-check">{{ $t('public.define') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listNotice,
    getNotice,
    delNotice,
    addNotice,
    updateNotice,
} from '@/api/system/notice'

export default {
    name: 'Notice',
    dicts: ['sys_notice_status', 'sys_notice_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 公告表格数据
            noticeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                noticeTitle: undefined,
                createBy: undefined,
                status: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                noticeTitle: [
                    {
                        required: true,
                        message: '公告标题不能为空',
                        trigger: 'blur',
                    },
                ],
                noticeType: [
                    {
                        required: true,
                        message: '公告类型不能为空',
                        trigger: 'change',
                    },
                ],
            },
            fullscreen: false,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询公告列表 */
        getList() {
            this.loading = true
            listNotice(this.queryParams).then((response) => {
                this.noticeList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                noticeId: undefined,
                noticeTitle: undefined,
                noticeType: undefined,
                noticeContent: undefined,
                status: '0',
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.noticeId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = this.$t('notice.addNotice')
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const noticeId = row.noticeId || this.ids
            getNotice(noticeId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = this.$t('notice.modNotice')
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.noticeId != undefined) {
                        updateNotice(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('public.modSuc'))
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addNotice(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('public.addSuc'))
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const noticeIds = row.noticeId || this.ids
            this.$modal
                .confirm(
                    this.$t('public.preConfirm', {
                        key: this.$t('notice.noticeCode'),
                        value: noticeIds,
                    })
                )
                .then(function () {
                    return delNotice(noticeIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('public.delSuc'))
                })
                .catch(() => {})
        },
    },
}
</script>
